package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Category;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理类别相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "05类别管理模块")
@Slf4j
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    public ICategoryService service;

    @ApiOperation("添加类别")
    @PostMapping("add-new")
    public String addNew(CategoryAddNewDTO categoryAddNewDTO){
        log.debug("开始处理类别请求,参数:{}",categoryAddNewDTO);
        service.addNew(categoryAddNewDTO);
        log.debug("添加类别成功");
        return "添加类别成功";
    }

    @ApiOperation("启用品牌")
    @ApiOperationSupport(order = 410)
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> Enable(@Range(min = 1,message = "删除品牌失败,尝试删除相册的ID无效")
                                   @PathVariable Long id){
        log.debug("开始处理启用类别服务");
        service.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用品牌")
    @ApiOperationSupport(order = 420)
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> Disable(@Range(min = 1,message = "删除品牌失败,尝试删除相册的ID无效")
                                    @PathVariable Long id){
        log.debug("开始处理启用类别服务");
        service.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 300)
    @GetMapping("")
    public JsonResult<List<CategoryListItemVO>> list(){
        log.debug("开始处理查看列表类别请求");
        List<CategoryListItemVO> list = service.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("显示在导航栏")
    @ApiOperationSupport(order = 430)
    @PostMapping("/{id:[0-9]+}/display")
    public JsonResult<Void> Display(@Range(min = 1)
                                   @PathVariable Long id){
        log.debug("开始处理启用类别服务");
        service.setDisplay(id);
        return JsonResult.ok();
    }

    @ApiOperation("不显示在导航栏")
    @ApiOperationSupport(order = 440)
    @PostMapping("/{id:[0-9]+}/hidden")
    public JsonResult<Void> Hidden(@Range(min = 1)
                                    @PathVariable Long id){
        log.debug("开始处理启用类别服务");
        service.setHidden(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询详情")
    @ApiOperationSupport(order = 310)
    @GetMapping("/{id:[0-9]+}/select")
    public JsonResult<CategoryStandardVO> selectById(@PathVariable Long id){
        CategoryStandardVO categoryStandardVO = service.selectById(id);
        return JsonResult.ok(categoryStandardVO);
    }

    @ApiOperation("修改详情")
    @ApiOperationSupport(order = 500)
    @GetMapping("/update")
    public JsonResult<Void> update(CategoryUpdateDTO categoryUpdateDTO){
        service.update(categoryUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@Range(min = 1,message = "删除类别失败,尝试删除类别的ID无效")
                             @PathVariable Long id){
        log.debug("开始处理品牌请求,参数:{}",id);
        service.delete(id);
        return JsonResult.ok();
    }


    // http://localhost:9080/categories/list-by-parent?parentId=0
    @ApiOperation("根据父级类别查询子级类别列表")
    @ApiOperationSupport(order = 315)
    @GetMapping("/list-by-parent")
    public JsonResult<List<CategoryListItemVO> > selectByParentId( Long parentId){
        log.debug("开始处理【根据父级类别查询子级类别列表】的请求");
        List<CategoryListItemVO> categoryListItemVOS = service.listByParentId(parentId);
        return JsonResult.ok(categoryListItemVOS);
    }

}
